const app = getApp()
const promisify = require("../../utils/promisify")

Page({
  data: {
    ratingExpress: {
      stars: [true, true, true, false, false],
      count: 3
    },
    ratingQuality: {
      stars: [true, true, true, false, false],
      count: 3
    },
    ratingService: {
      stars: [true, true, true, false, false],
      count: 3
    },
    orderId: undefined,
    confirmText: "发布",
    imgs: [],
    hasComment: false
  },
  onLoad: function (data) {
    let that = this
    let orderId = data.orderId
    orderId = orderId.slice(1, orderId.length - 1)
    that.setData({
      orderId: orderId
    })
    promisify(wx.request)({
      url: `https://www.cumthing.world/cumthing/rating/select` + app.ToParams({
        orderId: that.data.orderId,
        userId: app.globalData.userId
      }),
      method: "GET",
      header: {
        'Authorization': app.globalData.token
      }
    }).then(
      data => {
        data = data.data.data
        if (data) {
          that.setStars("ratingExpress", data.ratingExpress)
          that.setStars("ratingQuality", data.ratingQuality)
          that.setStars("ratingService", data.ratingService)
          that.setData({
            confirmText: "你已评过分了",
            hasComment: true
          })
        }
      }
    )
  },
  setStars: function (type, count) {
    let that = this
    let localData = that.data
    let data = localData[type]
    data.count = count
    data.stars.forEach((item, index) => {
      if (index <= count - 1) {
        data.stars[index] = true
      } else {
        data.stars[index] = false
      }
    })
    that.setData({
      [type]: data
    })
  },
  onShow: function () {

  },
  choose: function () {
    /**
     * 选择图片
     * 最多选4张
     */
    let that = this
    let count = 4 - that.data.imgs.length
    promisify(wx.chooseImage)({
      count: count,
      sizeType: ["compressed"],
      sourceType: ["album", "camera"]
    }).then(
      data => {
        let imgs = that.data.imgs
        imgs.push.apply(imgs, data.tempFilePaths)
        that.setData({
          imgs: imgs
        })
      },
      err => {
        console.log(err)
      }
    )
  },
  preview: function (e) {
    let that = this
    let current = e.target.dataset.img
    let urls = that.data.imgs
    wx.previewImage({
      current: current,
      urls: urls
    })
  },
  delete: function (e) {
    let that = this
    let index = e.target.dataset.index
    let imgs = that.data.imgs
    imgs.splice(index, 1)
    that.setData({
      imgs: imgs
    })
  },
  changeStar: function (e) {
    let that = this
    let index = e.target.dataset.index
    let type = e.target.dataset.type
    let stars = that.data[type].stars
    let count = 0
    stars.forEach((item, i) => {
      if (i <= index) {
        stars[i] = true
        count++
      } else {
        stars[i] = false
      }
    })
    that.setData({
      [type]: {
        stars: stars,
        count: count
      }
    })
  },
  confirm: function () {
    let that = this
    if (that.data.hasComment) {
      wx.showToast({
        title: "你已经评估分了",
        icon: "none",
        duration: 2000
      })
      return
    }
    promisify(wx.request)({
      url: `https://www.cumthing.world/cumthing/rating/add` + app.ToParams({
        orderId: that.data.orderId,
        userId: app.globalData.userId,
        ratingExpress: that.data.ratingExpress.count,
        ratingQuality: that.data.ratingQuality.count,
        ratingService: that.data.ratingService.count
      }),
      method: "GET",
      header: {
        'Authorization': app.globalData.token
      }
    }).then(
      data => {
        console.log(data.data)
        if (data.data.data.code === 0) {
          wx.showToast({
            title: "评分成功",
            icon: "none",
            duration: 2000
          })
          that.setData({
            hasComment: true,
            confirmText: "你已经评过分了"
          })
        }
      }
    )
    
    promisify(wx.uploadFile)({
      url: `https://www.cumthing.world/cumthing/upload/upload` + app.ToParams({
        file: that.data.imgs[0],
        userId: app.globalData.userId
      }),
      filePath: that.data.imgs[0],
      name: "file",
      header: {
        'Authorization': app.globalData.token,
        'content-type': 'multipart/form-data'
      }
    }).then(
      data => {
        console.log(data)
      },
      err => {
        console.log(err)
      }
    )
  }
})